package notwork;

import java.io.IOException;
import java.net.*;
import java.util.Scanner;

public class UdpEchoClient {
    private DatagramSocket socket=null;
    private String serverIp;
    private int serverPort;

    public UdpEchoClient(String serverIp, int serverPort) throws SocketException {
        this.serverIp = serverIp;
        this.serverPort = serverPort;
        socket=new DatagramSocket();
    }

    public void start() throws IOException {
        Scanner in=new Scanner(System.in);
        while(true){
            System.out.println("请输入要发送的内容：");
            if(!in.hasNext()){
                break;
            }
            String request=in.next();
            DatagramPacket requestPacket=new DatagramPacket(request.getBytes(),request.getBytes().length,
                    InetAddress.getByName(serverIp),serverPort);
            socket.send(requestPacket);

            DatagramPacket responsePacket=new DatagramPacket(new byte[4096],4096);
            socket.receive(responsePacket);
            String respon=new String(responsePacket.getData(),0, responsePacket.getLength());
            System.out.println(respon);

        }
    }

    public static void main(String[] args) throws IOException {
        UdpEchoClient client=new UdpEchoClient("47.108.28.88",9090);
        client.start();

    }
}
